package com.app.micaihu.custom.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;

/**
 * 自定义栏目展示滑动view
 */
public class ColumnHorizontalScrollView extends HorizontalScrollView {


    public int TABS_SHOW_TOTAL_COUNT_MIN = 3;//栏目宽度最平分3等份
    public int TABS_SHOW_TOTAL_COUNT_MAX = 7;//栏目宽度最大平分6等份

    /**
     * 传入整体布局
     */
    private View ll_content;
    /** 传入更多栏目选择布局 */
//	private View ll_more;
    /**
     * 传入拖动栏布局
     */
    private View rl_column;
    /**
     * 左阴影图片
     */
    private ImageView leftImage;
    /**
     * 右阴影图片
     */
    private ImageView rightImage;
    /**
     * 屏幕宽度
     */
    private int mScreenWitdh = 0;
    /**
     * 父类的活动activity
     */
    private Activity activity;

    private int mItemWidth;//栏目宽度

    public ColumnHorizontalScrollView(Context context) {
        super(context);
    }

    public ColumnHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ColumnHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 在拖动的时候执行
     */
    @Override
    protected void onScrollChanged(int paramInt1, int paramInt2, int paramInt3, int paramInt4) {
        // TODO Auto-generated method stub
        super.onScrollChanged(paramInt1, paramInt2, paramInt3, paramInt4);
        shade_ShowOrHide();
        if (!activity.isFinishing() && ll_content != null && leftImage != null && rightImage != null && rl_column != null) {
            if (ll_content.getWidth() <= mScreenWitdh) {
                leftImage.setVisibility(View.GONE);
                rightImage.setVisibility(View.GONE);
            }
        } else {
            return;
        }
        if (paramInt1 == 0) {
            leftImage.setVisibility(View.GONE);
            rightImage.setVisibility(View.VISIBLE);
            return;
        }
        if (ll_content.getWidth() - paramInt1 + rl_column.getLeft() == mScreenWitdh) {
            leftImage.setVisibility(View.VISIBLE);
            rightImage.setVisibility(View.GONE);
            return;
        }
        leftImage.setVisibility(View.VISIBLE);
        rightImage.setVisibility(View.VISIBLE);
    }

    /**
     * 传入父类布局中的资源文件
     */
    public void setParam(Activity activity, int mScreenWitdh, int columNum, View paramView1, ImageView paramView2, ImageView paramView3, View paramView5) {
        this.activity = activity;
        this.mScreenWitdh = mScreenWitdh;
        ll_content = paramView1;
        leftImage = paramView2;
        rightImage = paramView3;
//		ll_more = paramView4;
        rl_column = paramView5;
        setItemWidth(this.mScreenWitdh, columNum);
    }

    /**
     * 判断左右阴影的显示隐藏效果
     */
    public void shade_ShowOrHide() {
        if (!activity.isFinishing() && ll_content != null) {
            measure(0, 0);
            //如果整体宽度小于屏幕宽度的话，那左右阴影都隐藏
            if (mScreenWitdh >= getMeasuredWidth()) {
                leftImage.setVisibility(View.GONE);
                rightImage.setVisibility(View.GONE);
            }
        } else {
            return;
        }
        //如果滑动在最左边时候，左边阴影隐藏，右边显示
        if (getLeft() == 0) {
            leftImage.setVisibility(View.GONE);
            rightImage.setVisibility(View.VISIBLE);
            return;
        }
        //如果滑动在最右边时候，左边阴影显示，右边隐藏
        if (getRight() == getMeasuredWidth() - mScreenWitdh) {
            leftImage.setVisibility(View.VISIBLE);
            rightImage.setVisibility(View.GONE);
            return;
        }
        //否则，说明在中间位置，左、右阴影都显示
        leftImage.setVisibility(View.VISIBLE);
        rightImage.setVisibility(View.VISIBLE);
    }


    /**
     * 设置栏目宽度
     *
     * @param screenWitdh
     * @param columNum
     */
    public void setItemWidth(int screenWitdh, int columNum) {
        int columnCount;
        if (columNum > TABS_SHOW_TOTAL_COUNT_MAX) {
            columnCount = TABS_SHOW_TOTAL_COUNT_MAX;
        } else if (columNum < TABS_SHOW_TOTAL_COUNT_MIN) {
            columnCount = TABS_SHOW_TOTAL_COUNT_MIN;
        } else {
            columnCount = columNum;
        }
        mItemWidth = screenWitdh / columnCount;
    }

    /**
     * 返回栏目宽度
     *
     * @return
     */
    public int getItemWidth() {
        if (mItemWidth == 0) {
            mItemWidth = 150;//默认宽度
        }
        return mItemWidth;
    }

}
